package com.controller;

import com.model.*;
import com.response.Response;
import com.service.*;
import com.util.PageBean;
import com.util.removeHTML;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/api/orders")
public class OrdersController {

	@Resource
	private OrdersService ordersService;

	@Resource
	private StudentsService studentsService;

	@Resource
	private WorksService worksService;



	@Resource
	private RecordsService recordsService;

	// 订单列表
	@RequestMapping(value = "/list")
	@CrossOrigin
	public Response<List<Orders>> list(@RequestBody Orders orders, @RequestParam Integer currentPage,
			HttpServletRequest req) throws Exception {
		int pageSize = Integer.parseInt(req.getParameter("pageSize")); // 每页显示记录数
		int offset = (currentPage - 1) * pageSize; // 当前页开始记录
		int counts = 0; // 总记录数
		PageBean page = new PageBean(offset, pageSize); // 分页对象
		// 查询记录总数
		counts = ordersService.getCount(orders);
		// 获取当前页记录
		List<Orders> ordersList = ordersService.queryOrdersList(orders, page);

		int page_count = counts % PageBean.PAGE_IETM == 0 ? counts / PageBean.PAGE_IETM
				: counts / PageBean.PAGE_IETM + 1; // 总页数
		return Response.success(ordersList, counts, page_count);
	}

	// 订单列表
	@RequestMapping(value = "/list2")
	@CrossOrigin
	public Response<List<Orders>> list2(Orders orders, @RequestParam Integer currentPage,
			HttpServletRequest req) throws Exception {
		int pageSize = Integer.parseInt(req.getParameter("pageSize")); // 每页显示记录数
		int offset = (currentPage - 1) * pageSize; // 当前页开始记录
		int counts = 0; // 总记录数
		PageBean page = new PageBean(offset, pageSize); // 分页对象
		// 查询记录总数
		counts = ordersService.getCount(orders);
		// 获取当前页记录
		List<Orders> ordersList = ordersService.queryOrdersList(orders, page);

		int page_count = counts % PageBean.PAGE_IETM == 0 ? counts / PageBean.PAGE_IETM
				: counts / PageBean.PAGE_IETM + 1; // 总页数
		return Response.success(ordersList, counts, page_count);
	}

	// 添加订单
	@ResponseBody
	@PostMapping(value = "/add")
	@CrossOrigin
	public Response add(Orders orders, HttpServletRequest req) throws Exception {
		try {

			Students students = studentsService.queryStudentsById(orders.getSno());
			Works works = worksService.queryWorksById(orders.getWid());

			SimpleDateFormat df = new SimpleDateFormat("yyMMddHHmmssSSS");
			String no = df.format(new Date()).toString();

			orders.setNo(no);

			// 得到当前时间 格式：yyyy-MM-dd HH:mm:ss
			String odate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
			orders.setOjd(odate + " 用户：" + students.getSname() + "，手机号码：" + students.getTel() + "，已接单");
			ordersService.insertOrders(orders);

			// 修改任务状态
			works.setFlag("已接单");
			worksService.updateWorks(works);

		} catch (Exception e) {
			return Response.error();
		}
		return Response.success();
	}

	// 删除订单
	@ResponseBody
	@PostMapping(value = "/del")
	@CrossOrigin
	public Response del(HttpServletRequest req) throws Exception {
		try {

			String id = req.getParameter("id");
			String f = req.getParameter("f");
			if (f != null && f.equals("1")) {
				Orders orders1 = ordersService.queryOrdersById(id);

				orders1.setFlag("已送达");

				// 得到当前时间 格式：yyyy-MM-dd HH:mm:ss
				String odate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

				orders1.setOjd(odate + " 接收人已送达<br>" + orders1.getOjd());

				ordersService.updateOrders(orders1);
			} else if (f != null && f.equals("2")) {
				
				Orders orders1 = ordersService.queryOrdersById(id);

				Students students = studentsService.queryStudentsById(orders1.getSno());

				orders1.setFlag("已完成");

				// 得到当前时间 格式：yyyy-MM-dd HH:mm:ss
				String odate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

				orders1.setOjd(odate + " 发布人确认任务完成<br>" + orders1.getOjd());

				ordersService.updateOrders(orders1);

				// 更新用户余额
				students.setUval(Double.parseDouble(students.getUval().toString())
						+ Double.parseDouble(orders1.getPrice().toString()));

				studentsService.updateStudents(students);

				// 添加记录
				Records records = new Records();
				records.setSno(orders1.getSno());
				records.setRtype("收入");
				records.setRmemo("完成任务获得佣金：" + orders1.getPrice() + "元");

				recordsService.insertRecords(records);

			} else {
				ordersService.deleteOrders(id); // 删除
			}

		} catch (Exception e) {
			return Response.error();
		}
		return Response.success();
	}

	// 修改订单
	@ResponseBody
	@PostMapping(value = "/update")
	@CrossOrigin
	public Response update(@RequestBody Orders orders, HttpServletRequest req) throws Exception {
		try {
			ordersService.updateOrders(orders); // 修改
		} catch (Exception e) {
			return Response.error();
		}
		return Response.success();
	}

	// 修改订单
	@ResponseBody
	@PostMapping(value = "/update2")
	@CrossOrigin
	public Response update2(Orders orders, HttpServletRequest req) throws Exception {
		try {
			ordersService.updateOrders(orders); // 修改
		} catch (Exception e) {
			return Response.error();
		}
		return Response.success();
	}

	// 返回订单详情
	@ResponseBody
	@PostMapping(value = "/get")
	@CrossOrigin
	public Response get(HttpServletRequest req) throws Exception {
		try {
			String id = req.getParameter("id");
			Orders orders = ordersService.queryOrdersById(id); // 根据ID查询
			return Response.success(orders);
		} catch (Exception e) {
			return Response.error();
		}

	}

}
